<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;

class ArticleController extends CommonController {

	
	private function getcate(){
		$cate = M('Category');
		$catelist=$cate->getField('id,name');
		$this->assign('catelist',$catelist);
		
	}

	public function index(){

		$this->getcate();
		parent::index(true);
	}

	
	public function modify(){
		$this->getcate();
		parent::modify();
	}

	public function update(){
		$m=D('Article');
		$upload = new \Think\Upload();   // 实例化上传类
		$upload->maxSize   =     3145728 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath  =      '/images/'; // 设置附件上传目录
			
		// 上传文件
		$info   =   $upload->uploadOne($_FILES['fileselect']);
		if(!$info) {// 上传错误提示错误信息
			unset($_POST['img']);
		}else{// 上传成功 获取上传文件信息
			$_POST['img'] = '/Uploads'.$info['savepath'].$info['savename'];
			$imginfo = $m-> where('id='.$_POST['id'])->getField('img');
			//删除 原来的 图片
			if(is_file(__ROOT__.$imginfo)) {
				//echo "删除 员图片 ";
				unlink(__ROOT__.$imginfo);
			}
		}
			
		if(!$m->create()){
			$this->show('error');
		}
		$idNum=$m->save();
		if($idNum>0){
			$this->show('ok');
		}else{
			$this->show('error');
		}
	}

	public function add(){
		$this->getcate();
		$this->display('Article/add');
	}
	
	public function create(){
			
		$upload = new \Think\Upload();   // 实例化上传类
		$upload->maxSize   =     3145728 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath  =      '/images/'; // 设置附件上传目录
		// 上传文件
		$info   =   $upload->uploadOne($_FILES['fileselect']);
		if($info) {// 上传错误提示错误信息
			$_POST['img'] = '/Uploads'.$info['savepath'].$info['savename'];
		}
			
		$m=D('Article');
		if(!$m->create()){
			$this->show('error');
		}
		$idNum=$m->add();
		if($idNum>0){

			$this->show('ok');
		}else{
			$this->show('error');
		}
	}



}